import { NestFactory, Reflector } from '@nestjs/core';
import { AppModule } from './app.module';
import { ClassSerializerInterceptor, ValidationPipe } from '@nestjs/common';
import { TypeOrmExceptionFilter } from './services/typeorm-exception.filter';
import { HttpExceptionFilter } from './services/http-exception.filter';
import { ResponseInterceptor } from './services/response.interceptors';
import * as cookieParser from 'cookie-parser';
import * as session from 'express-session';
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.enableCors({
    origin: '*',
  });
  app.use(cookieParser());
  // session
  app.use(
    session({
      secret: 'wl-better-me',
      resave: false,
      saveUninitialized: false,
    }),
  );
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true, // 剔除 DTO 中没有声明的属性
      forbidNonWhitelisted: true, // 若存在未声明的属性，则抛出错误
      transform: true, // 自动转换 payload 为 DTO 实例
    }),
  );
  app.useGlobalFilters(new TypeOrmExceptionFilter());
  app.useGlobalFilters(new HttpExceptionFilter());
  app.useGlobalInterceptors(new ResponseInterceptor());
  app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));
  await app.listen(3000);
}
bootstrap();
